﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Kugar.Core.ExtMethod;
using Newtonsoft.Json.Linq;
using RabbitMQ.Client;

namespace Kugar.Tool.RemoteRPC.ServiceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var connFactory = new ConnectionFactory()
            {
                HostName = "203.195.191.64",
  
                Port = 5672,
                VirtualHost = "developtest",
                UserName = "ylcoretest",
                Password = "123456",
                AutomaticRecoveryEnabled = true,
                RequestedHeartbeat = 10,
                NetworkRecoveryInterval = new TimeSpan(0, 0, 10)
            };

            var conn = connFactory.CreateConnection();

            

            var rpc = new RemoteRPC(conn, "RPCServicTest");

            rpc.ServiceInvoke += Rpc_ServiceInvoke;

            rpc.AddServiceHander("SendSMS");

            Console.ReadLine();
        }

        private static int _count = 0;

        private static void Rpc_ServiceInvoke(object sender, ServiceInvokeEventArgs e)
        {
            var c = Interlocked.Increment(ref _count);

            

            Console.WriteLine($"触发{e.ServiceName},次数:{c}");

            if (c == 2 || c==6)
            {
                Thread.Sleep(50000);

            }

            e.ReturnValue=new JObject()
            {
                ["Test"]=e.Arguments.GetString("Arg1") + "_" +  c
            };
        }
    }
}
